// @Date         2021-11-21
// @Author       Aaron Zhang
// @Description	 创建中间件，命令的模板
// @FileName      template.go

package template

import (
	"zin/framework/util"
)

var contractTmp = `// @Date         {{.date}}
// @Author       Aaron Zhang
// @Description   {{.desc}}
// @FileName      {{.name}}.go   

package {{.name}}

const ProviderKey{{.name|title}} = "{{.name}}"

type IContract{{.name|title}} interface {
   // 请在这里定义你的方法
}
`

var providerTmp = `// @Date         {{.date}}
// @Author       Aaron Zhang
// @Description   {{.desc}}
// @FileName      {{.name}}.go   


package {{.name}}
import (
   "zin/framework"
)

type Provider{{.name|title}} struct {
  
}

func (sp *Provider{{.name|title}}) Name() string {
   return ProviderKey{{.name|title}}
}

func (sp *Provider{{.name|title}}) Register(c framework.Container) framework.NewInstance {
   return NewProviderService {{.name|title}}
}

//isDefer控制什么时候初始化
func (sp *Provider{{.name|title}}) IsDefer() bool {
   return {{.defer}}
}

func (sp *Provider{{.name|title}}) Params(params map[string]interface{}) map[string]interface{} {
   return params
}

// boot负责配置初始化
func (sp *Provider{{.name|title}}) Boot(c framework.Container) error {
   return nil
}
`

var serviceTmp = `// @Date         {{.date}}
// @Author       Aaron Zhang
// @Description   {{.desc}}
// @FileName      {{.name}}.go   


package {{.name}}
import (
  "zin/framework"
)

type ProviderService{{.name|title}} struct {
}

func NewProviderService{{.name|title}}(params map[string]interface{}) (interface{}, error) {

	return &ProviderService{{.name|title}}{
		
	}, nil
}

`

func CreateContractByTemplate(pName string, data map[string]interface{}) error {
	providerFolder := util.GetAppProvider().ProviderFolder()
	return putContentToFileByTemplate(pName, "contract", contractTmp, providerFolder, data)
}

func CreateProviderByTemplate(pName string, data map[string]interface{}) error {
	providerFolder := util.GetAppProvider().ProviderFolder()
	return putContentToFileByTemplate(pName, "provider", providerTmp, providerFolder, data)
}

func CreateProviderServiceByTemplate(pName string, data map[string]interface{}) error {
	providerFolder := util.GetAppProvider().ProviderFolder()
	return putContentToFileByTemplate(pName, "service", serviceTmp, providerFolder, data)
}
